package com.dzf.dao;

import com.dzf.entity.Blog;
import com.dzf.entity.Rate;
import com.dzf.entity.RespJson;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class RateDao {
    @Autowired
    MongoTemplate mongoTemplate;

    public RespJson likeBlog(String uid, String blogid){
        String msg = "";
        int code = 0;
        Object date = null;
        Update update = new Update();
        //拿到这条博客的数据，等待后续修改
        Blog blog = mongoTemplate.findById(blogid, Blog.class);
        Rate rate = mongoTemplate.findOne(new Query(Criteria.where("blogid").is(blogid)), Rate.class);
        System.out.println(rate);
        //如果没有这条数据就会添加
        if (rate==null)
        {
            blog.setLikenums(1);
            List<Object> list = new ArrayList<>();
            list.add(uid);
            Rate rate1 = new Rate();
            rate1.setBlogid(blogid);
            rate1.setLikeUser(list);
            msg="点赞成功！！";
            code=200;
            date=true;
            //博客保存点赞数据
            Blog blog1 = mongoTemplate.save(blog);
            Rate rate2 = mongoTemplate.save(rate1);
            return RespJson.success(code,msg,date);
        }else
        {
            Boolean flag = true;
            List<String> likeUser = (List<String>) rate.getLikeUser();
            for (int i = 0; i < likeUser.size(); i++) {
                if (likeUser.get(i).equals(uid))
                {

                    blog.setLikenums(blog.getLikenums()-1);
                    //如果该博客数据下已经有这个用户id，删除（取消点赞）
                    flag=false;
                    likeUser.remove(i);
                    msg="取消点赞";
                    code=200;
                    date=true;
                }
            }
            if (flag){
                blog.setLikenums(blog.getLikenums()+1);
                //如果没有，加入（点赞）
                likeUser.add(uid);
                msg="点赞成功！！";
                code=200;
                date=true;
            }
            System.out.println(likeUser);
            update.set("likeUser",likeUser);
        }
        //博客保存点赞数据
        Blog blog1 = mongoTemplate.save(blog);
        //更新rate表
        UpdateResult result = mongoTemplate.updateFirst(new Query(Criteria.where("blogid").is(blogid)), update, Rate.class);
        return RespJson.success(code,msg,date);
    }

    public boolean findByUserLike(String blogid, String uid){
        Rate rate = mongoTemplate.findOne(new Query(Criteria.where("blogid").is(blogid)), Rate.class);
       if (rate!=null)
       {
           List<String> likeUser = (List<String>) rate.getLikeUser();
           for (String id:
                   likeUser) {
               if (id.equals(uid))
               {
                   return true;
               }
           }
       }
        return false;
    }


}
